﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class AdjustmentDA : IAdjustmentDA
    {
        private string ADJUSTMENT_DATE = "@ADJUSTMENT_DATE";
        private string ADJUSTMENT_ID = "@ADJUSTMENT_ID";
        private string CREATED_BY = "@CREATED_BY";
        private string IS_ACTIVE = "@IS_ACTIVE";
        private string OWNER_ID = "@OWNER_ID";
        private string REMARK = "@REMARK";
        private string SQL_DELETE_ADJUSTMENT = " DELETE FROM @_@ADJUSTMENT WHERE ADJUSTMENT_ID = @ADJUSTMENT_ID ";
        private string SQL_INSERT_ADJUSTMENT = " INSERT INTO @_@ADJUSTMENT ( WH_ID, ADJUSTMENT_ID, OWNER_ID, ADJUSTMENT_DATE, IS_ACTIVE, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE,REMARK) VALUES ( @WH_ID, @ADJUSTMENT_ID, @OWNER_ID, @ADJUSTMENT_DATE, @IS_ACTIVE, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE(),@REMARK )  ";
        private string SQL_SELECT_ALL_ADJUSTMENT = " SELECT ROWID,WH_ID,ADJUSTMENT_ID,OWNER_ID,ADJUSTMENT_DATE,IS_ACTIVE,TS,CREATED_BY,CREATED_DATE,UPDATED_BY,UPDATED_DATE,REMARK FROM  @_@ADJUSTMENT ";
        private string SQL_UPDATE_ADJUSTMENT = " UPDATE @_@ADJUSTMENT SET WH_ID = @WH_ID, OWNER_ID = @OWNER_ID, ADJUSTMENT_DATE = @ADJUSTMENT_DATE, IS_ACTIVE = @IS_ACTIVE, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE(),REMARK=@REMARK WHERE ADJUSTMENT_ID = @ADJUSTMENT_ID  ";
        private string TS = "@TS";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public int DeleteAdjustment(List<string> adjustmentID, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int j = 0; j < adjustmentID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ADJUSTMENT_ID", adjustmentID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_ADJUSTMENT.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public AdjustmentInfo GetAdjustmentByID(string adjustmentID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_ADJUSTMENT.Replace("@_@", whLoginID) + " WHERE ADJUSTMENT_ID = @ADJUSTMENT_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ADJUSTMENT_ID", adjustmentID) };
            AdjustmentInfo adjustmentInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    DateTime CS00003;
                    adjustmentInfo = new AdjustmentInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["ADJUSTMENT_ID"].ToString(), reader["OWNER_ID"].ToString(), (reader["ADJUSTMENT_DATE"].ToString() != "") ? DateTime.Parse(reader["ADJUSTMENT_DATE"].ToString()) : (CS00003 = new DateTime()), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00003 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime(), reader["REMARK"].ToString());
                }
            }
            return adjustmentInfo;
        }

        public AdjustmentInfo GetAdjustmentByID(DataBase dataBase, DbTransaction tran, string adjustmentID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_ADJUSTMENT.Replace("@_@", whLoginID) + " WHERE ADJUSTMENT_ID = @ADJUSTMENT_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ADJUSTMENT_ID", adjustmentID) };
            AdjustmentInfo adjustmentInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                DateTime CS00003;
                adjustmentInfo = new AdjustmentInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["ADJUSTMENT_ID"].ToString(), reader["OWNER_ID"].ToString(), (reader["ADJUSTMENT_DATE"].ToString() != "") ? DateTime.Parse(reader["ADJUSTMENT_DATE"].ToString()) : (CS00003 = new DateTime()), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00003 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime(), reader["REMARK"].ToString());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return adjustmentInfo;
        }

        public DataSet GetAdjustmentByQueryList(List<string> sqlWhere, AdjustmentQueryEntity adjustmentQuery, string whLoginID, PubParasInfo pubParasInfo)
        {
            string temp = "SELECT DISTINCT A.ROWID,A.WH_ID,A.ADJUSTMENT_ID,A.OWNER_ID,A.ADJUSTMENT_DATE,A.IS_ACTIVE,A.TS,A.CREATED_BY,A.CREATED_DATE,A.UPDATED_BY,A.UPDATED_DATE,A.REMARK,B.RECEIPT_ID FROM  @_@ADJUSTMENT A LEFT JOIN @_@ADJUSTMENT_DETAIL B ON A.ADJUSTMENT_ID=B.ADJUSTMENT_ID  INNER JOIN SYSADM.USER_OWNER UO ON A.OWNER_ID = UO.OWNER_ID AND UO.[USER_ID] = @USER_ID ".Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!adjustmentQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, adjustmentQuery.CurrentPage, adjustmentQuery.PageSize, adjustmentQuery.SortField, adjustmentQuery.SortDirection).Replace("@USER_ID", "''" + pubParasInfo.UserID + "''");
            }
            else
            {
                temp = temp.Replace("@USER_ID", "'" + pubParasInfo.UserID + "'");
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetAllAdjustment(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_ADJUSTMENT.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public int InsertAdjustment(DataBase dataBase, DbTransaction tran, AdjustmentInfo adjustmentInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                adjustmentInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_Adjustment_Parameters(adjustmentInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_ADJUSTMENT.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public string ReceiptRevert(AdjustmentInfo adjustmentInfo)
        {
            throw new NotImplementedException();
        }

        private SqlParameter[] Set_Adjustment_Parameters(AdjustmentInfo adjustmentInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.ADJUSTMENT_ID, SqlDbType.VarChar, 20), new SqlParameter(this.OWNER_ID, SqlDbType.VarChar, 20), new SqlParameter(this.ADJUSTMENT_DATE, SqlDbType.DateTime, 8), new SqlParameter(this.IS_ACTIVE, SqlDbType.VarChar, 1), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.REMARK, SqlDbType.VarChar, 200) };
            if (!string.IsNullOrEmpty(adjustmentInfo.WhID))
            {
                paramArray[0].Value = adjustmentInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = adjustmentInfo.AdjustmentID;
            paramArray[2].Value = adjustmentInfo.OwnerID;
            paramArray[3].Value = adjustmentInfo.AdjustmentDate;
            paramArray[4].Value = adjustmentInfo.IsActive;
            paramArray[5].Value = adjustmentInfo.CreatedBy;
            paramArray[6].Value = adjustmentInfo.UpdatedBy;
            paramArray[7].Value = adjustmentInfo.Remark;
            return paramArray;
        }

        public int UpdateAdjustment(DataBase dataBase, DbTransaction tran, AdjustmentInfo adjustmentInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                adjustmentInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_Adjustment_Parameters(adjustmentInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_ADJUSTMENT.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

